# coding=utf-8

import requests
from lxml import etree
import time
import socket
import csv
from selenium import webdriver
from configparser import ConfigParser
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By

# 禁止图片和css加载
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 如果想加载图片，就把下面第二句话改第一句话，删掉上面的“禁止图片和css加载”部分
# wb = Chrome(options=option)
wb = webdriver.Chrome(options=chrome_options)

# 最大化窗口、输入网址、等待至网页加载完成（防止元素还没加载出来就开始爬了这样自然爬不到数据。如果一直加载不出就等10秒，加载好了就立刻结束等待）
wb.maximize_window()
wb.get("https://weibo.com/login.php")
wb.implicitly_wait(20)


#
# yonghu = '17685457791'
# pwd = 'AAaa1234'
# # 点击登录、输入账号密码、点击确认登录
# wb.find_element(By.XPATH, '//div[@class="gn_login"]/ul/li[last()]/a').click()
# wb.find_element(By.XPATH, '//div[@class="item username input_wrap"]/input').send_keys(yonghu)
# wb.find_element(By.XPATH, '//div[@class="item password input_wrap"]/input').send_keys(pwd)
# wb.find_element(By.XPATH, '//div[@class="item_btn"]/a').click()
#
# # 点击发送短信验证
# wb.find_element(By.XPATH, 'messageCheck').click()



# 方式一：输入六位验证码
# pycharm里输入6位验证码，并填入。 方式一我就做到这，感兴趣的可以补充完整
# 点击发送验证码、pycharm里输入验证码、填入六位验证码
# wb.find_element_by_id('message_sms_login').click()
# yanzheng=input("输入六位验证码：")
# for i in range(0,6):
#     wb.find_element_by_xpath('//div[@class="num clearfix"]/input[{}]'.format(i+1)).send_keys("yanzheng[i]")

# 方式二：其他号码登录、填号码、下一步、提交
# wb.find_element(By.ID, 'toAddPhoneNum').click()
# wb.find_element(By.ID, 'AphoneNumber').send_keys(yonghu)
# wb.find_element(By.ID, 'addPhoneNext').click()
# qqqqq = input("发送短信后任意输入：")
# wb.find_element(By.ID, 'mobileLogin').click()
# 登录成功


index = "甜食"
sheng = 44
year = 2020
month = 1
day1 = 1
day2 = 30
page = 1

url = "https://s.weibo.com/weibo?q={}&region=custom:{}:1000&typeall=1&suball=1&timescope=custom:{}-{}-{}:{}-{}-{}&Refer=g&page={}".format(
    index, sheng, year, month, day1, year, month, day2, page)
wb.get(url)

# 输入为csv文件的路径
outputExcel = r'D:\file\Python' + '\\' + 'python'

while month < 13:
    # 发布时间的列表time2 ,博文的列表 textAll
    time2 = []
    textAll = []
    flag = 0

    # 爬取当前时间段全部页的内容
    for yeshu in range(1, 51):
        page = yeshu
        try:
            url = "https://s.weibo.com/weibo?q={}&region=custom:{}:1000&typeall=1&suball=1&timescope=custom:{}-{}-{}:{}-{}-{}&Refer=g&page={}".format(
                index, sheng, year, month, day1, year, month, day2, page)
            wb.get(url)
            wb.implicitly_wait(2)

            # 错误检测
            try:
                errorTxt = ""
                merror = wb.find_element(By.XPATH, '//div[@class="card card-no-result s-pt20b40"]/p')
                errorTxt = merror.text
                # print(errorTxt)
                if (errorTxt[0] == '抱' and errorTxt[1] == '歉'):
                    flag += 1
                if (flag > 4):
                    break
                continue
            except:
                pass
            # 爬取时间
            time1 = wb.find_elements(By.XPATH,
                                     '//div[@class="card-feed"]/div[@class="content"]/p[@class="from"]/a[last()-1]')
            for i in time1:
                time2.append(i.text)

            # 先点击所有的展开原文
            temp = wb.find_elements(By.XPATH,
                                    '//div[@class="card-feed"]/div[@class="content"]/p[@node-type="feed_list_content"]/a[@action-type="fl_unfold"]')
            for i in temp:
                if (i.text[0] == "展" and i.text[1] == "开" and i.text[2] == "全"):
                    i.click()

            texttp = wb.find_elements(By.XPATH,
                                      '//div[@class="card-feed"]/div[@class="content"]/p[@node-type="feed_list_content_full"]')
            for i in texttp:
                temp1 = ""
                temp = i.text
                for j in temp:
                    temp1 = temp1 + j
                textAll.append(temp1)
            # 如果没有展开全文按钮，就去没有展开全文按钮的段落爬
            texttp = wb.find_elements(By.XPATH,
                                      '//div[@class="card-feed"]/div[@class="content"]/p[@node-type="feed_list_content"]')
            for i in texttp:
                temp1 = ""
                temp = i.text
                for j in temp:
                    temp1 = temp1 + j
                # print("leng=",len(temp1),temp1)
                if (len(temp1) > 0):
                    textAll.append(temp1)

            time.sleep(1.1)
        except:
            continue
            # 爬取完50页

    # 导出csv文件
    for a, b in zip(textAll, time2):
        try:
            print(a)
            with open(outputExcel + '.csv', 'a', newline='') as f:
                csvwriter = csv.writer(f, dialect='excel')
                csvwriter.writerow([a, b])
        except:
            continue
    time.sleep(2)

    month += 1
    day2 = 30
    if month == 2:
        day2 = 28
    elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
        day2 = 31
    else:
        day2 = 30
